﻿/**
 *       Project: ServerLib
 *     Copyright: Copyright (C) 2008, Rob Kennedy
 *   Create Date: 2008-07-15 3:40 PM
 * Modified Date: 2008-07-15 3:40 PM    RK  Added comments
 *      Comments: Ignore SA1633.
 *      
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace ServerLib.Data
{
    using System.Data;
    using Data.mySql;
    using Data.Oracle;
    using Data.SqlServer;

    /// --------------------------------------------------------------------
    /// Project	 : ServerLib
    /// Class	 : Database
    /// Inherits : None.
    /// --------------------------------------------------------------------
    /// <summary>
    /// Builds a database connection to a specific type of DBMS 
    /// </summary>
    /// <authorname>Rob Kennedy</authorname>
    /// <remarks></remarks>
    /// --------------------------------------------------------------------
    public class Database
    {
        #region Private Properties

        /// <summary>
        /// Contains the IDatabase interface class for the current DBMS
        /// </summary>
        private IDatabase DB;

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the database connection string data
        /// </summary>
        public ConnectionStringBuilder ConnectionString
        {
            get
            {
                return DB.ConnectionString;
            }
            set
            {
                DB.ConnectionString = value;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the database connection is connected
        /// </summary>
        public bool IsConnected
        {
            get
            {
                return DB.IsConnected;
            }
        }

        /// <summary>
        /// Gets any database error message returned
        /// </summary>
        public string ErrorMessage
        {
            get
            {
                return DB.ErrorMessage;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Opens a database connection
        /// </summary>
        /// <returns>True if the database connection was a success</returns>
        public bool Open()
        {
            return this.DB.Open();
        }

        /// <summary>
        /// Opens a database connection
        /// </summary>
        /// <param name="connectionString">A connection string to use for opening</param>
        /// <returns>True if the database connection was a success</returns>
        public bool Open(string connectionString)
        {
            return this.DB.Open(connectionString);
        }

        /// <summary>Closes a database connection</summary>
        public void Close()
        {
            this.DB.Close();
        }

        /// <summary>
        /// Executes a SQL command and returns results in a DataTable object 
        /// </summary>
        /// <param name="sqlCommand">The SQL command to execute</param>
        /// <returns>If no error occurs a DataTable object is returned, otherwise null on error</returns>
        DataTable SelectQuery(string sqlCommand)
        {
            return DB.SelectQuery(sqlCommand);
        }

        /// <summary>
        /// Executes a SQL command and returns results in a DataTable object 
        /// </summary>
        /// <param name="sqlCommand">The SQL command to execute</param>
        /// <param name="parameterValues">Optional parameters to insert into the SQL command string</param>
        /// <returns>If no error occurs a DataTable object is returned, otherwise null on error</returns>
        DataTable SelectQuery(string sqlCommand, params object[] parameterValues)
        {
            return DB.SelectQuery(sqlCommand, parameterValues);
        }

        /// <summary>
        /// Executes a SQL command and returns a single row in a DataRow object
        /// </summary>
        /// <param name="sqlCommand">The SQL command to execute</param>
        /// <returns>If no error occurs a DataRow object is returned, otherwise null on error</returns>
        DataRow SelectQueryRow(string sqlCommand)
        {
            return DB.SelectQueryRow(sqlCommand);
        }

        /// <summary>
        /// Executes a SQL command and returns a single row in a DataRow object
        /// </summary>
        /// <param name="sqlCommand">The SQL command to execute</param>
        /// <param name="rowIndex">A specific row index of a number of expected row results to return</param>
        /// <param name="parameterValues">Optional parameters to insert into the SQL command string</param>
        /// <returns>If no error occurs a DataRow object is returned, otherwise null on error</returns>
        DataRow SelectQueryRow(string sqlCommand, int rowIndex, params object[] parameterValues)
        {
            return DB.SelectQueryRow(sqlCommand, rowIndex, parameterValues);
        }

        /// <summary>
        /// Executes a SQL command and returns the top most row in a DataRow object
        /// </summary>
        /// <param name="sqlCommand">The SQL command to execute</param>
        /// <returns>If no error occurs a DataRow object is returned, otherwise null on error</returns>
        DataRow SelectQueryTopRow(string sqlCommand)
        {
            return DB.SelectQueryTopRow(sqlCommand);
        }

        /// <summary>
        /// Executes a SQL command and returns the top most row in a DataRow object
        /// </summary>
        /// <param name="sqlCommand">The SQL command to execute</param>
        /// <param name="parameterValues">Optional parameters to insert into the SQL command string</param>
        /// <returns>If no error occurs a DataRow object is returned, otherwise null on error</returns>
        DataRow SelectQueryTopRow(string sqlCommand, params object[] parameterValues)
        {
            return DB.SelectQueryTopRow(sqlCommand, parameterValues);
        }

        /// <summary>
        /// Executes a SQL command and returns a boolean value based on either the result or the record
        /// count of results.
        /// </summary>
        /// <param name="sqlCommand">The SQL command to execute</param>
        /// <param name="useRecordCount">If true, the check will use the query results record count for results</param>
        /// <param name="parameterValues">Optional parameters to insert into the SQL command string</param>
        /// <returns>Returns true if the SQL command resulted in a true state or returned one or more rows</returns>
        bool QueryCheck(string sqlCommand, bool useRecordCount, params object[] parameterValues)
        {
            return DB.QueryCheck(sqlCommand, useRecordCount, parameterValues);
        }

        /// <summary>
        /// Executes a SQL command and returns the number of rows effected.
        /// </summary>
        /// <param name="sqlCommand">The SQL command to execute</param>
        /// <param name="parameterValues">Optional parameters to insert into the SQL command string</param>
        /// <returns>Returns the number of rows effected by the SQL command</returns>
        int ExecuteQuery(string sqlCommand, params object[] parameterValues)
        {
            return DB.ExecuteQuery(sqlCommand, parameterValues);
        }

        /// <summary>
        /// Executes a SQL command and returns the value of the specified field in the results.
        /// </summary>
        /// <param name="sqlCommand">The SQL command to execute</param>
        /// <param name="valueField">The name of the result field to retrive a value from</param>
        /// <param name="parameterValues">Optional parameters to insert into the SQL command string</param>
        /// <returns>Returns the value of the specified field in the top most row of any data results or null on no results</returns>
        object QueryValue(string sqlCommand, string valueField, params object[] parameterValues)
        {
            return DB.QueryValue(sqlCommand, valueField, parameterValues);
        }

        /// <summary>
        /// Retrieves the current id of any identity column in the data table specified.
        /// </summary>
        /// <param name="tableName">The table name to retrieve an identity value from</param>
        /// <returns>Returns the current identity value of a table, 0 if no identity exists.</returns>
        int GetCurrentIdentity(string tableName)
        {
            return DB.GetCurrentIdentity(tableName);
        }

        #endregion

        #region Private Methods
        
        /// <summary>
        /// Builds the current database provider based on a given provider name
        /// </summary>
        /// <param name="providerName">Base database provider name to use</param>
        /// <param name="connectionString">Base database connection string to use</param>
        private void BuildDatabaseProvider(string providerName, string connectionString)
        {
            switch (providerName.ToLower())
            {
                case "mysql.data.mysqlclient":      // mysql
                    DB = new MySqlDatabase();
                    break;
                case "system.data.oracleclient":    // Oracle
                    DB = new OracleDatabase();
                    break;
                case "system.data.sqlclient":       // SQL Server
                default:
                    DB = new SqlServerDatabase();
                    break;
            }

        }
        #endregion
    }
}
